package CarEngine;

import com.jme.input.controls.GameControl;
import com.jme.input.controls.GameControlManager;
import com.jme.input.controls.binding.KeyboardBinding;
import com.jme.input.controls.binding.MouseButtonBinding;
import com.jme.math.Vector3f;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import static CarEngine.CubeController.CubeAction.*;
import static com.jme.input.KeyInput.*;
import static com.jme.input.controls.binding.MouseButtonBinding.*;
 
 
public class CubeController extends Controller {
 
    enum CubeAction {LEFT, RIGHT, UP, DOWN, TOP, BOTTOM, EXIT};
 
    private final static float SPEED = 2F;
 
    private final Node node;
    private final GameControlManager manager;
    private float vAngle = 0F;
    private float hAngle = 0F;
 
    public CubeController(Node node) {
        this.node = node;
        this.manager = new GameControlManager();
 
        //create all actions
        for (CubeAction action : CubeAction.values()) {
            manager.addControl(action.name());
        }
        //bind keys
        bindKey(EXIT, KEY_X);
        bindKey(UP, KEY_UP);
        bindKey(DOWN, KEY_DOWN);
        bindKey(LEFT, KEY_LEFT);
        bindKey(RIGHT, KEY_RIGHT);
        bindKey(TOP, KEY_W);
        bindKey(BOTTOM, KEY_S);
        //bind mouse buttons
        bindMouseButton(LEFT, LEFT_BUTTON);
        bindMouseButton(RIGHT, RIGHT_BUTTON);
    }
 
 
    private void bindKey(CubeAction action, int... keys) {
        final GameControl control = manager.getControl(action.name());
        for (int key : keys) {
          control.addBinding(new KeyboardBinding(key));
        }
    }
 
    private void bindMouseButton(CubeAction action, int mouseButton) {
        final GameControl control = manager.getControl(action.name());
        control.addBinding(new MouseButtonBinding(mouseButton));
    }
 
    private float value(CubeAction action) {
        return manager.getControl(action.name()).getValue();
    }
 
    @Override
    public void update(float time) {
        if (value(EXIT) > 0) {
            System.exit(0); //OK, this is just a demo...
        }
        //hAngle += SPEED * time * (value(RIGHT) - value(LEFT));
        //vAngle += SPEED * time * (value(DOWN) - value(UP));
        //node.getLocalRotation().fromAngles(vAngle, hAngle, 0f);
        node.getLocalTranslation().addLocal(SPEED * time * (value(RIGHT) - value(LEFT)), SPEED * time * (value(UP)-value(DOWN)), SPEED * time * (value(TOP)-value(BOTTOM)));
        Vector3f nv = node.getLocalTranslation();
        System.out.println(nv.x+"Y:  " +nv.y +"Z: "+ nv.z);
        //System.out.println(time+"hanlge: "+hAngle+"vAngle: "+vAngle);
    }
}

